package com.xasz.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xasz.entity.Candidate;
import com.xasz.entity.Jury;
import com.xasz.entity.Sum;
import com.xasz.service.IJuryService;
import com.xasz.service.ISumService;
import com.xasz.service.ex.DeleteException;
import com.xasz.service.impl.ActivityServiceImpl;
import com.xasz.service.impl.SumServiceImpl;
import com.xasz.util.JsonResult;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author cai
 * @since 2022-08-01
 */
@AllArgsConstructor
@RestController
@RequestMapping("sum")
public class SumController  extends BaseController{
    private ISumService iSumService;
    private SumServiceImpl sumService;
    private IJuryService juryService;
    /**
     * 通过活动id查询总分记录
     */
    @GetMapping
    public  JsonResult<List<Sum>>  getSumById(Integer activityId, HttpSession session){
        //权限判断
        if (!juryService.getOne(new QueryWrapper<Jury>().eq("activity_id",activityId).eq("jury_id",getUidFromSession(session))).getName()
                .equals(getUsernameFromSession(session))){
            throw new DeleteException("你没有权限查看该活动维度信息");
        }
        //查询
        QueryWrapper<Sum> wrapper=new QueryWrapper<Sum>()
                .eq("activity_id",activityId);
        return new JsonResult<>(OK,iSumService.list(wrapper));
    }
    /**
     * 直接进行求和更新总分
     */
    @PutMapping("{Aid}")
    public  JsonResult<Void>  updateSum(@PathVariable Integer Aid, HttpSession session){
        sumService.upDataSum(Aid,getUidFromSession(session),getUsernameFromSession(session));
        return new JsonResult<>(OK);
    }










    //超级管理员专用查询所有总分记录
    @GetMapping("super")
    public  JsonResult<List<Sum>>  getSum(){
        return new JsonResult<>(OK,iSumService.list());
    }


//    @PostMapping("{id}")
//    public  JsonResult<Void>  inputSum(@PathVariable Integer id, HttpSession session){
//        sumService.creatSum(id);
//        return new JsonResult<>(OK);
//    }

//    @DeleteMapping
//    public  JsonResult<Void>  DeleteSum(@RequestBody Sum sum, HttpSession session){
//        sumService.detectSum(sum.getActivityId(),sum.getCandidateId());
//        return new JsonResult<>(OK);
//    }
}
